package com.zdf.license.web.controller;

import com.zdf.license.verify.annotation.VLicense;
import com.zdf.license.web.listener.Server1TimeVerifyListener;
import com.zdf.license.web.listener.UserNumVerifyListener;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {

    private final UserNumVerifyListener userNumVerifyListener;
    private final Server1TimeVerifyListener server1TimeVerifyListener;
    public TestController(UserNumVerifyListener userNumVerifyListener, Server1TimeVerifyListener server1TimeVerifyListener){
        this.userNumVerifyListener = userNumVerifyListener;
        this.server1TimeVerifyListener = server1TimeVerifyListener;
    }

    /**
     * 校验用户数量的小例子
     */
    private int userNum = 10;
    @RequestMapping("/1")
    public AjaxResult<?> test1(){
        boolean flag = userNumVerifyListener.verify(userNum + 1);
        if (flag) {
            userNum ++;
            return AjaxResult.success("校验通过。", userNum);
        }
        return AjaxResult.error("校验失败。", userNum);
    }

    /**
     * 校验当前程序是否已经注册
     */
    @VLicense
    @RequestMapping("/2")
    public AjaxResult<?> test2(){
        return AjaxResult.success("已经注册");
    }

    /**
     * 校验xx是否已过期
     */
    @RequestMapping("/3")
    public AjaxResult<?> test3(){
        if (server1TimeVerifyListener.verify()) {
            return AjaxResult.success("时间尚未过期。");
        }
        return AjaxResult.error("时间已过期。");
    }

}
